package com.ruoyi.web.controller.learn;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.service.ILearningContentService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LikeTable;
import com.ruoyi.system.service.ILikeTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 记录点赞人信息Controller
 * 
 * @author ruoyi
 * @date 2024-03-08
 */
@Controller
@RequestMapping("/system/like")
public class LikeTableController extends BaseController
{
    private String prefix = "system/table";

    @Autowired
    private ILikeTableService likeTableService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ILearningContentService contentService;

    @RequiresPermissions("system:like:view")
    @GetMapping()
    public String table()
    {
        return prefix + "/table";
    }

    /**
     * 查询记录点赞人信息列表
     */
    @RequiresPermissions("system:like:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LikeTable likeTable)
    {
        startPage();
        List<LikeTable> list = likeTableService.selectLikeTableList(likeTable);
        for (LikeTable table : list) {
            table.setUserName(userService.selectUserById(table.getUserId()).getUserName());
            table.setContentTitle(contentService.selectLearningContentByContentId(table.getContentId()).getTitle());
        }
        return getDataTable(list);
    }

    /**
     * 导出记录点赞人信息列表
     */
    @RequiresPermissions("system:like:export")
    @Log(title = "记录点赞人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LikeTable likeTable)
    {
        List<LikeTable> list = likeTableService.selectLikeTableList(likeTable);
        ExcelUtil<LikeTable> util = new ExcelUtil<LikeTable>(LikeTable.class);
        return util.exportExcel(list, "记录点赞人信息数据");
    }

    /**
     * 新增记录点赞人信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存记录点赞人信息
     */
    @RequiresPermissions("system:like:add")
    @Log(title = "记录点赞人信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Map<String,Integer> addSave(@RequestBody LikeTable likeTable)
    {
        likeTable.setUserId(getUserId());
        likeTable.setLikeTime(new Date());
        likeTableService.insertLikeTable(likeTable);
        LikeTable query = new LikeTable();
        query.setContentId(likeTable.getContentId());
        List<LikeTable> likeTables = likeTableService.selectLikeTableList(query);
        Map<String,Integer> map = new HashMap<>();
        map.put("likeNum",likeTables.size());
        return map;
    }

    /**
     * 修改记录点赞人信息
     */
    @RequiresPermissions("system:like:edit")
    @GetMapping("/edit/{likeId}")
    public String edit(@PathVariable("likeId") Long likeId, ModelMap mmap)
    {
        LikeTable table = likeTableService.selectLikeTableByLikeId(likeId);
        table.setUserName(userService.selectUserById(table.getUserId()).getUserName());
        table.setContentTitle(contentService.selectLearningContentByContentId(table.getContentId()).getTitle());
        mmap.put("likeTable", table);
        return prefix + "/edit";
    }

    /**
     * 修改保存记录点赞人信息
     */
    @RequiresPermissions("system:like:edit")
    @Log(title = "记录点赞人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LikeTable likeTable)
    {
        return toAjax(likeTableService.updateLikeTable(likeTable));
    }

    /**
     * 删除记录点赞人信息
     */
    @RequiresPermissions("system:like:remove")
    @Log(title = "记录点赞人信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(likeTableService.deleteLikeTableByLikeIds(ids));
    }
}
